if(typeof G.goods == "undefined") {
    G.goods = {};
}


G.goods.init = function() {
    E.ajax_get({
        action: "goods",
        operFlg: 6,
        data: {
            id: this.postID,
            source: 'wap'
        },
        call: "G.goods.display"
    });
}

G.goods.display = function( o ) {
    if (o.code == 200) {

        if (o.data.goodsList.length == 1) {
            $("body").append("<input type='hidden' value='0' id='size_id_" + this.postID + "' />");
            $("#size_id_" + this.postID).data(o.data.goodsList[0]);
            this.sizeChange(this.postID, o.data.goodsList[0]);
        } else {
            var id = "";
            $.each(o.data.goodsList, function(k, v) {
                if (v.useFlg == 1 && v.forsale == 1 && v.enableSaleAmount > 0) {
                    if (id == "" || v.postID == G.goods.postID)
                        id = v.postID;
                    $("#size_id_" + v.postID).data(v);
                } else {
                    $("#size_id_" + v.postID).addClass("stockout").removeAttr("onclick");
                }
            });
            if (id != "") {
                this.sizeChange(id);
            } else {
                $('#addCart').html('<a href="javascript: void(0);" class="tc over">暂时缺货</a>').removeClass('hide');
            }
        }

    }
}
G.goods.init();

G.goods.sizeChange = function( id, dt ) {

    if (!dt) {
        dt = $("#size_id_" + id).data();
        $("#size_list").find("a.on").removeClass("on");
        $("#size_id_" + id).addClass("on");
    }
    if (dt.enableSaleAmount == 0 || dt.useFlg == 0 || dt.forsale == 0) {
        $('#addCart').html('<a href="javascript: void(0);" class="tc over">暂时缺货</a>').removeClass('hide');
    } else {
        if (dt.price) {
            $("#price").text('¥' + parseFloat(dt.price).toFixed(2));
            $('#addCart').html('<a href="javascript: G.goods.nowbuy();" class="tc quk_buy">立即购买</a>&nbsp;&nbsp;<a href="javascript: G.goods.addToCart();"  class="tc add_cart">加入购物车</a>').removeClass('hide');
        } else {
            $("#price").text('¥' + parseFloat(dt.pointPrice).toFixed(2) + '+' + parseInt(dt.addPoints) + '良票');
            $('#addCart').html('<a href="javascript: G.goods.points.addCart(1);"  class="tc add_cart">良票兑换</a>').removeClass('hide');
        }
    }
    $("#salePrice").text('¥' + parseFloat(dt.marketPrice).toFixed(2));
    $("#pro_id").val(dt.postID);
    if (dt.enableSaleAmount > 0) {
        $("#inventory").removeClass('hide').find('span').text('有货');
    } else {
        $("#inventory").removeClass('hide').find('span').text('无货');
    }

    if (dt.promotion || dt.bill) {
        var html = "";
        if (dt.promotion) {
            $.each(dt.promotion, function(k, v) {
                if (v.type == 1) {
                    html += "<li>[立减] " + v.remark + "</li>";
                } else if (v.type == 2) {
                    html += "<li>[特价] " + v.remark + "</li>";
                } else if (v.type == 4) {
                    html += "<li>[良票] " + v.remark + "</li>";
                } else if (v.type == 5) {
                    $.each(v.gift, function(k1, v1) {
                        html += "<li>[赠品] " + v1.goodsName + " x " + v1.saleAmount + "</li>";
                    });
                }
            });
        }
        if (dt.bill) {
            $.each(dt.bill, function(k, v) {
                if (v.type == 1) {
                    html += "<li>[满减] " + v.remark + "</li>";
                } else if (v.type == 2) {
                    html += "<li>[免运费] " + v.remark + "</li>";
                } else if (v.type == 4) {
                    html += "<li>[优惠券] " + v.remark + "</li>";
                }
            });
        }

        $("#promotion").html(html).show();
        $('#promotion_box').removeClass('hide');
    } else {
        $("#promotion").html("").hide();
        $('#promotion_box').addClass('hide');
    }

}


//商品数量的增减
G.goods.reduce = function() {
    var goods_amount = $("#goods_amount").val();
    goods_amount = parseInt(goods_amount);
    if(!goods_amount || !E.isInt(goods_amount) || (goods_amount <= 1)){
        goods_amount = 1;
        $("#goods_amount").val(goods_amount);
    }else{
        goods_amount -= 1;
        $("#goods_amount").val(goods_amount);
    }
}

G.goods.plus = function() {
    var dt = $("#size_id_" + $("#pro_id").val()).data();
    var goods_amount = $("#goods_amount").val();
    goods_amount = parseInt(goods_amount);
    if(E.isInt(goods_amount) && (goods_amount < 99)) {
        if (dt.enableSaleAmount < goods_amount) {
            $("#goods_amount").val(dt.enableSaleAmount);
        } else {
            goods_amount += 1;
            $("#goods_amount").val(goods_amount);
        }
    }
}

//加入 购物车
G.goods.addToCart = function() {

    var goods_amount = E.trim($("#goods_amount").val());
    if (!E.isInt(goods_amount)) {
        $("#goods_amount").val(1);
        E.alert("您输入的数量有误，请重新输入");
    } else {
        cartTrace();//加入购物车跟踪代码
        G.cart.general.add($("#pro_id").val(), goods_amount);
    }

}

//立即购买
G.goods.nowbuy = function() {

    var goods_amount = E.trim($('#goods_amount').val());
    if (!E.isInt(goods_amount)) {
        $('#goods_amount').val(1);
        E.alert('您输入的数量有误，请重新输入');
    } else {
        G.cart.general.buy($('#pro_id').val(), goods_amount);
    }

}

/**
 * 商品收藏
 * @type {{add: Function, result: Function}}
 */
G.goods.collection = {

    add: function() {
        if (G.cust.cust_id == null) {
            self.location = G.args.login;
        } else {
            E.ajax_post({
                action: "customer",
                operFlg: 4,
                data:{
                    postID : G.goods.postID
                },
                call: "G.goods.collection.result"
            });
        }

    },


    result: function( o ) {

        if (o.code == 200 ){
            E.alert(o.message, 2);
        } else {
            E.alert(o.message);
        }

    }

};

//积分商品
G.goods.points = {

    //添加
    addCart: function( flg ){

        if (G.cust.cust_id == null) { //判断用户登录

            self.location = G.args.login;

        } else {

            var goods_amount = E.trim($("#goods_amount").val());
            if (!E.isInt(goods_amount)) {
                $("#goods_amount").val(1);
                E.alert("您输入的数量有误，请重新输入");
            } else {
                G.cart.general.flg = flg;
                E.loadding.open("正在添加商品到购物车，请稍候...");
                E.ajax_post({
                    action: "cart",
                    operFlg: 13,
                    data: {
                        postID: $("#pro_id").val(),
                        goods_amount: goods_amount
                    },
                    call: "G.cart.general.result"
                });
            }
        }
    }
};

